import sys
from os import path
pwd = path.dirname(__file__)
sys.path.append(pwd)

import xlrd
import myUtils
from ast import literal_eval

class Excels:

    def __init__(self, name='Sheet1', file=myUtils.dirname('data.xlsx', 'data')):
        """
        初始excel表
        :param sname: excel表名
        :param file: excel文件路径
        """
        with xlrd.open_workbook(file) as book:
            self.sheet = book.sheet_by_name(name)

    def row_values(self, row):
        """
        获取row行的值，其中首行不做处理，4和5列将字符串类型转成字典类型
        :param row:
        :return: row行的值
        """
        if row == 0:
            return self.sheet.row_values(row)
        else:
            row_list = self.sheet.row_values(row)
            if row_list[4] != '':
                row_list[4] = literal_eval(row_list[4])
                row_list[4]['key'] = myUtils.read_yaml('gaode')['key']
            if row_list[5] != '':
                row_list[5] = literal_eval(row_list[5])
            return row_list

    def req_data(self, start_raw = 1, num = 0):
        """
        默认获取所有的测试用例请求数据
        :param start_raw: 测试开始的行数,且要大于0的整数
        :param num: 测试用例的数量
        :return:
        """
        data_list = []
        if start_raw > 0:
            if num < 1:
                for row in range(start_raw, self.sheet.nrows):
                    data_list.append(self.row_values(row))
            else:
                for row in range(start_raw, start_raw + num):
                    data_list.append(self.row_values(row))
            return data_list


if __name__ == '__main__':
    data = Excels()
    print(len(data.req_data()))



